package it.slkfarm.helpers;



import it.slkfarm.bean.Farmer;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FieldsControl 
{
	public static boolean[] checkBean(Farmer farmer, String phoneNumber)
	{
		int len;
		Pattern pat;
		Matcher match;
		boolean[] result = new boolean[6];
		
		
		//Name
		if (farmer.getFirstname().equalsIgnoreCase(""))
			result[1] = true;
		else
		{
			pat = Pattern.compile("[a-z|A-Z|\\s]{3,30}");
			match = pat.matcher(farmer.getFirstname());
			result[1] = match.matches();
		}
		
		//Lastname
		if (farmer.getLastname() == null)
			result[2] = false;
		else
		{
			pat = Pattern.compile("[a-z|A-Z|\\s]{3,30}");
			match = pat.matcher(farmer.getLastname());
			result[2] = match.matches();
		}
		
		//Mail
		if (farmer.getEmail().equalsIgnoreCase(""))
			result[3] = true;
		else
		{
			pat = Pattern.compile("^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\\-+)|([A-Za-z0-9]+\\."
					+ "+)|([A-Za-z0-9]+\\++))*[A-Za-z0-9]+@((\\w+\\-+)|(\\w+\\.))*\\w{1,63}\\." + 
					"[a-zA-Z]{2,6}$");
			match = pat.matcher(farmer.getEmail());
			result[3] = match.matches();
		}
		
		//Pin
		if (farmer.getPin().equalsIgnoreCase(""))
			result[4] = false;
		else
		{
			len = farmer.getPin().length();
			if (len != 4)
				result[4] = false;
			else
				result[4] = true;
		}
		
		//Number	
		if (phoneNumber.equalsIgnoreCase(""))
			result[5] = false;
		else
		{
			len = phoneNumber.length();
			if (len != 10)
				result[5] = false;
			else
				result[5] = true;
		}
		
		//General
		result[0] = true;
		for (int i=1; i<6; i++)
			result[0] = result[0] & result[i];
		return result;
			
	}
}
